/**
 * Created by hsia on 2017/12/28
 */
var BindCoupon = function($parent) {
	this.$parent = $parent;
	this.sysUtils = new kendoUtils();
	this.init();
	return this;
}
BindCoupon.prototype = {
		init : function() {
			var fieldArray = this.sysUtils.getBindFieldArray(this.$parent);
			var functionArray = [
			                     {fName : "search_btn", fs : this.findData},
			                     {fName : "reset_btn", fs : this.resetInfo}
			                     ];
			this.bindBaseModels = new bindBaseModel(null, fieldArray, functionArray, null);
			this.sysUtils.bindDoucment(this.bindBaseModels, this.$parent, this);
			this.createList();
			this.findData();
		},
		/*
		 * 重置按钮
		 */
		resetInfo:function(e){
			var that = $(e.target).data("that");
			that.sysUtils.setBindFieldNull(that.bindBaseModels.bindFieldArray);
		},
		deleteBind : function(e) {
			var _this = this;
			if (!isEmpty(e)) {
				_this = $(e.target).data('that');
			}
			var ids = "";
			_this.$parent.find('input[name=id]:checked').each(function(inx, itm) {
				ids += $(itm).val() + ",";
			});
			if (ids == "") {
				notification({title : "操作失败", message : "请至少选择一条数据来删除"}, 'info');
				return;
			}
			ids = ids.substring(0, ids.length-1);
			notification({title:"确认信息", message:"确定需要删除选中的信息？"}, 'confirm', {
				callback : function() {
					JsonpAjax(baseApiPath + "cimc/deleteBind?ids="+ids, undefined, "json", function(res) {
						_this.findData();
					});
				}
			});
		},
		findData : function(e) {
			var _this = this;
			if (!isEmpty(e)) {
				_this = $(e.target).data('that');
			}

			var params = _this.sysUtils.getBindDocJson(_this.bindBaseModels.bindFieldArray);
			var dataT = {jsonParam : JSON.stringify(params)};
			var dataSource = _this.sysUtils.getDataSource({
				url : baseApiPath + "cimc/coupons",
				data : dataT,
				dataType : "odata"
			});
			var grid = _this.$parent.find('#bindCoupon_data_list').data('kendoGrid');
			grid.setDataSource(dataSource);
		},
		createList : function() {
			var _this = this;
			var columns = [
			               {
			            	   width:30,
			            	   headerTemplate : '<input type="checkbox" gridId="bindCoupon_data_list" onclick="gridCheckAll(this)" />',
			            	   template : '<input type="checkbox" name="id" value="${id}" />'
			               },
			               {
			            	   title: "优惠劵码",
			            	   field: "preferentiaIdent"
			               },
			               {
			            	   title: "车辆凭证",
			            	   field: "bingVoucher"
			               },
			               {
			            	   title : "优惠类型",
			            	   template:'#:Utils.getModelNameByParentKey(BASIS.PREFERENTIAL_TYPE.ROOT, preferentiaType)#'
			               },
			               {
			            	   title : "隶属项目 (编号)",
			            	   template : '${data.preferentialName} <small>(${data.preferentialCode})</small>'
			               },
			               {
			            	   title : "入园时间",
			            	   field : "inTime"
			               },
			               {
			            	   title : "赠送时间",
			            	   field : "bingtime"
			               }
			               ];
			_this.sysUtils.createGird(_this.$parent.find('#bindCoupon_data_list'), {
				selectable : 'single',
				autoSync : true,
				columnMenu : true,
				pageable : {input : true, numeric : false},
				columns : columns,
				change : function(e) {
					var selected = $.map(this.select(), function(itm) {
						return $(itm).find("input[type=checkbox]").val();
					});
					_this.selectRowId = selected[0];
				}
			});
		},
		openBindWindow : function(e) {
			var _this = this;
			if (!isEmpty(e)) {
				_this = $(e.target).data('that');
			}
			_this.editWindow = _this.sysUtils.openWindow(_this.editWindow, {
				title : "车辆绑定优惠券",
				width : "700px",
				height : "280px",
				resizable : false,
				content : "views/parkLotCenter/discountManage/bindCoupon_edit.html"
			});
		}
}
var isValid = false;
var tipMsg='',recodeId='',preferentialName='';
var BindCouponEdit = function($parent) {
	this.$parent = $parent;
	this.sysUtils = new kendoUtils();
	this.init();
	return this;
}
BindCouponEdit.prototype = {
		init : function() {
			var fieldArray = this.sysUtils.getBindFieldArray(this.$parent);
			var functionArray = [
			                     {fName : "save_btn", fs : this.saveInfo},
			                     {fName : "cancel", fs : function() { $('#' + bindCoupon.editWindow).data('kendoWindow').close() }},
			                     ];
			this.bindBaseModels = new bindBaseModel(null, fieldArray, functionArray, null);
			this.sysUtils.bindDoucment(this.bindBaseModels, this.$parent, this);
			this.loadInfo();
		},
		loadInfo:function () {
			var that = this;
			JsonpAjax(baseApiPath+"cimc/projects",null,"json",function(result){
				that.createProjectsDropDownList({div:that.$parent.find("#preferentialCode"),select:result,unshift:true},that);
			},true);
			that.$parent.find("#bindVoucher")[0].onblur=function () {
				var thisOne = this;
				var voucher = thisOne.value;
				if(isNotEmpt(voucher)){
					JsonpAjax(baseApiPath+'cimc/checkVoucher?voucher='+voucher,null,"json",function(result){
						if(result){
							if(result.isIn){
								if(result.isBind){
									tipMsg='车辆凭证'+voucher+'已绑定过优惠券！！！';
									layer.tips(tipMsg,thisOne,{tipsMore: true,tips: [2, '#78BA32']});
								}else{
									recodeId=result.recodeId;
									//进出通道、凭证类型
									that.sysUtils.mvvmModel.set("inTime",result.intime);
									that.sysUtils.mvvmModel.set("chnName",result.chnName);
									that.sysUtils.mvvmModel.set("credentialsName",Utils.getModelNameByParentKey(BASIS.TRAFFIC_TYPE_CLASS.ROOT, result.credentialsType));
								}
							}else{
								tipMsg='车辆凭证'+voucher+'车辆当前不在园内！！！';
								layer.tips(tipMsg,thisOne,{tipsMore: true,tips: [2, '#78BA32']});
								thisOne.value='';
							}
						}
					});
				}
			}
		},
		saveInfo : function(e) {
			var _this = this;
			if (!isEmpty(e)) {
				_this = $(e.target).data('that');
			}
			if(validityForm(_this.$parent)){
				var bindData = _this.sysUtils.getBindDocJson();
				bindData['recodeId']=recodeId;
				bindData['preferentialName']=preferentialName;
				JsonpAjax(baseApiPath + "cimc/saveBind", JSON.stringify(bindData), "json", function(res) {
					bindCoupon.findData();
					$('#' + bindCoupon.editWindow).data('kendoWindow').close();
				});
			}
		},
		createProjectsDropDownList:function (json,that,callback) {
			var dataSource = json["select"];
			if(json["unshift"]){
				dataSource.unshift({preferentialName:"--请选择--",preferentialCode:""});
			}
			var dropDownList = json["div"].kendoDropDownList({
				dataTextField: "preferentialName",
				dataValueField: "preferentialCode",
				dataSource: dataSource,
				change : function(e) {
					var dataItem = this.dataItem(e.item);
					preferentialName=dataItem.preferentialName;
					//优惠项目类型，优惠额度
					that.sysUtils.mvvmModel.set("preferentialContent",dataItem.preferentialContent);
					that.sysUtils.mvvmModel.set("preferentialType",Utils.getModelNameByParentKey(BASIS.PREFERENTIAL_TYPE.ROOT, dataItem.preferentialType));
				}
			}).data("kendoDropDownList");
			if(null != json["dataBound"] && typeof json["dataBound"] =="function"){
				dropDownList.bind("dataBound",json["dataBound"]);
			}
			if(null != json["change"] && typeof json["change"] =="function"){
				dropDownList.bind("change",json["change"]);
			}
			//回调最外层函数
			if(null != callback &&  typeof callback =="function"){
				callback.apply(that);
			}
		}
}